package view;

import java.awt.*;
import java.sql.SQLException;

import javax.swing.*;
import javax.swing.LayoutStyle.*;
import javax.swing.GroupLayout.*;

import controller.*;
import estado.*;

import model.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PeliculaCopiaAlta extends JPanel {
	private static final long serialVersionUID = 1L;
	private JComboBox<String> peliculaCopiaAltaAnyo = new JComboBox<String>();
	private JComboBox<String> peliculaCopiaAltaMes = new JComboBox<String>();
	private JComboBox<String> peliculaCopiaAltaDia = new JComboBox<String>();

	/**
	 * Create the panel.
	 */
	public PeliculaCopiaAlta(final Pelicula pelicula) {
		setSize(new Dimension(300, 200));
		
		JLabel peliculaCopiaAltaTit = new JLabel("Cargar Copia de Pelicula");
		peliculaCopiaAltaTit.setAlignmentX(Component.CENTER_ALIGNMENT);
		peliculaCopiaAltaTit.setHorizontalAlignment(SwingConstants.CENTER);
		peliculaCopiaAltaTit.setHorizontalTextPosition(SwingConstants.CENTER);
		peliculaCopiaAltaTit.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		JLabel peliculaCopiaAltaNombre = new JLabel("Pelicula:");
		peliculaCopiaAltaNombre.setHorizontalAlignment(SwingConstants.RIGHT);
		JLabel peliculaCopiaAltaNivelador = new JLabel("");
		
		JLabel peliculaCopiaAltaFechaAdquisicionLbl = new JLabel("Fecha de adquisici\u00F3n:");
		peliculaCopiaAltaFechaAdquisicionLbl.setHorizontalAlignment(SwingConstants.RIGHT);

		final Integer anio=Integer.parseInt(pelicula.getFechaEstreno().substring(0, 4));
		final Integer mes=Integer.parseInt(pelicula.getFechaEstreno().substring(5, 7));
		final Integer dia=Integer.parseInt(pelicula.getFechaEstreno().substring(8, 10));
		for(Integer i=anio;i<=2020;i++)peliculaCopiaAltaAnyo.addItem(i.toString());
		for(Integer i=mes;i<=12;i++)peliculaCopiaAltaMes.addItem(i.toString());
		for(Integer i=dia;i<=31;i++)peliculaCopiaAltaDia.addItem(i.toString());
		JLabel peliculaCopiaAltaFechaBarra1 = new JLabel("/");
		JLabel peliculaCopiaAltaFechaBarra2 = new JLabel("/");
		peliculaCopiaAltaAnyo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(peliculaCopiaAltaAnyo.getItemCount() != 0){
					if(peliculaCopiaAltaMes.getItemCount() > 0)peliculaCopiaAltaMes.removeAllItems();
					if(anio == Integer.parseInt((String) peliculaCopiaAltaAnyo.getSelectedItem())){
						for(Integer i=mes;i<=12;i++)peliculaCopiaAltaMes.addItem(i.toString());
					}else{
						for(Integer i=1;i<=12;i++)peliculaCopiaAltaMes.addItem(i.toString());
					}
				}
			}
		});
		peliculaCopiaAltaMes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(peliculaCopiaAltaMes.getItemCount() != 0){
					if(peliculaCopiaAltaDia.getItemCount() > 0)peliculaCopiaAltaDia.removeAllItems();
					if(mes == Integer.parseInt((String) peliculaCopiaAltaMes.getSelectedItem())){
						for(Integer i=dia;i<=31;i++)peliculaCopiaAltaDia.addItem(i.toString());
					}else{
						for(Integer i=1;i<=31;i++)peliculaCopiaAltaDia.addItem(i.toString());
					}
				}
			}
		});
		
		JButton peliculaCopiaAltaBtn = new JButton("Cargar");
		peliculaCopiaAltaBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String fechaAdquisicion=(String)peliculaCopiaAltaAnyo.getSelectedItem()+"-"+(String)peliculaCopiaAltaMes.getSelectedItem()+"-"+(String)peliculaCopiaAltaDia.getSelectedItem();
				EstadoCopia estado=EstadoController.definirEstado(1);
				PeliculaCopia peliculaCopia=new PeliculaCopia(null,pelicula,fechaAdquisicion,estado);
				try{
					PeliculaController.getPeliculaController().guardarCopia(peliculaCopia);
				}catch(SQLException e){
				}
			}
		});
		
		JLabel peliculaCopiaNom = new JLabel(pelicula.getNombre());
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(356)
							.addComponent(peliculaCopiaAltaNivelador, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(peliculaCopiaAltaNombre, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
								.addComponent(peliculaCopiaAltaFechaAdquisicionLbl, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(peliculaCopiaNom)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(peliculaCopiaAltaAnyo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(peliculaCopiaAltaFechaBarra1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(peliculaCopiaAltaMes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(peliculaCopiaAltaFechaBarra2, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE)
									.addGap(4)
									.addComponent(peliculaCopiaAltaDia, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addComponent(peliculaCopiaAltaBtn))
							.addPreferredGap(ComponentPlacement.RELATED, 139, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(peliculaCopiaAltaTit, GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGap(0))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(peliculaCopiaAltaTit, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
					.addGap(19)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(peliculaCopiaAltaNombre)
							.addComponent(peliculaCopiaNom))
						.addComponent(peliculaCopiaAltaNivelador))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(peliculaCopiaAltaAnyo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(peliculaCopiaAltaFechaAdquisicionLbl)
							.addComponent(peliculaCopiaAltaFechaBarra1)
							.addComponent(peliculaCopiaAltaMes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(peliculaCopiaAltaFechaBarra2))
						.addComponent(peliculaCopiaAltaDia, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(peliculaCopiaAltaBtn)
					.addContainerGap(65, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}
}
